package gov.pbc.nn.kjgl.software.handle;

import gov.pbc.nn.core.dao.CommonJdbcDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.AssignmentHandler;
import com.bstek.uflo.process.node.TaskNode;

@Component("projectAdminAssignmentHandler")
public class ProjectAdminAssignmentHandler implements AssignmentHandler {

	@Override
	public Collection<String> handle(TaskNode taskNode,
			ProcessInstance processInstance, Context context) {
		String projectId = processInstance.getBusinessId();
		String sql = "select a.USER_NAME_ from t_project_member as a,t_project_change as b where a.PROJECT_ID_=b.PROJECT_ID_ and a.PROJECT_ROLE_=0 and a.INVALID_=0 and b.ID_=?";
		CommonJdbcDao jdbc = ContextHolder.getBean(CommonJdbcDao.BEAN_ID);
		List<String> users = jdbc.getJdbcTemplate().query(sql,
				new String[] { projectId }, new RowMapper<String>() {
					@Override
					public String mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						// TODO Auto-generated method stub
						String s = rs.getString("USER_NAME_");
						return s;
					}
				});
		if (!users.isEmpty()) {
			return users;
		}
		return null;
	}
}
